﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Compiled.Win8StartWPF
{
	/// <summary>
	/// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
	///
	/// Step 1a) Using this custom control in a XAML file that exists in the current project.
	/// Add this XmlNamespace attribute to the root element of the markup file where it is 
	/// to be used:
	///
	///     xmlns:MyNamespace="clr-namespace:Compiled.Win8StartWPF"
	///
	///
	/// Step 1b) Using this custom control in a XAML file that exists in a different project.
	/// Add this XmlNamespace attribute to the root element of the markup file where it is 
	/// to be used:
	///
	///     xmlns:MyNamespace="clr-namespace:Compiled.Win8StartWPF;assembly=Compiled.Win8StartWPF"
	///
	/// You will also need to add a project reference from the project where the XAML file lives
	/// to this project and Rebuild to avoid compilation errors:
	///
	///     Right click on the target project in the Solution Explorer and
	///     "Add Reference"->"Projects"->[Browse to and select this project]
	///
	///
	/// Step 2)
	/// Go ahead and use your control in the XAML file.
	///
	///     <MyNamespace:CustomControl1/>
	///
	/// </summary>
	public class FolderPanel : ShortcutPanel
	{
		public FolderPanel()
			: base()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(FolderPanel), new FrameworkPropertyMetadata(typeof(FolderPanel)));
		}

		private string _topFolder = "";
		public string TopFolder
		{
			get { return _topFolder; }
			set { _topFolder = value; }
		}

		private string _folder = "";
		public string Folder
		{
			get { return _folder; }
			set
			{
				if (System.IO.Directory.Exists(value))
				{
					_folder = value;
					if (TopFolder == "") TopFolder = value;
					LoadFolder(_folder);
				}
			}
		}

		protected virtual void LoadFolder(string Path)
		{
			Clear();

			if (TopFolder != Folder)
				AddItem(System.IO.Path.GetDirectoryName(Path), "..", Properties.Resources.Folder);

			string[] folders = System.IO.Directory.GetDirectories(Path);
			foreach (string folder in folders)
			{
				AddFolder(folder);
			}

			string[] files = System.IO.Directory.GetFiles(Path);
			foreach (string file in files)
			{
				AddFile(file);
			}
		}

		protected virtual void AddFile(string Path)
		{
			string extension = System.IO.Path.GetExtension(Path).ToLower();
			if (extension == ".ini")
				return;

			Shortcut s = new Shortcut(Path);
			AddItem(Path, s.Name, s.Icon);
		}

		protected virtual void AddFolder(string Path)
		{
			AddItem(Path, System.IO.Path.GetFileName(Path), Properties.Resources.Folder);
		}

		protected virtual void AddItem(string Path, string Text, System.Drawing.Icon Icon)
		{
			IconButton ib = new IconButton();
			ib.Text = Text;
			ib.Data = Path;
			ib.Icon = Icon.ToBitmap();
			Add(ib);
		}

		public override void Item_Clicked(IconButton sender)
		{
			if (System.IO.Directory.Exists(sender.Data))
				this.Folder = sender.Data;
			else
				Launch(sender.Data);
		}

		public virtual void Launch(string FileName)
		{
			try
			{
				Shortcut s = new Shortcut(FileName);
				s.Launch();
			}
			catch (Exception ex)
			{
				System.Diagnostics.Debug.WriteLine(ex.ToString());
				MessageBox.Show(ex.ToString());
			}
		}
	}
}
